package TeXing.src.Day1.ZuoYe;

/*4：传闻StringBuilder比StringBuffer快,比较一下，输出你的比较结果（拼接10万个相同的字符串，比较时间）。
 * */
public class Compare {
    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();
        StringBuilder builder = new StringBuilder();
        long f = System.nanoTime();
        for (int i = 0; i < 100000; buffer.append(i), i++) ;
        long f1 = System.nanoTime();
        long d = System.nanoTime();
        for (int i = 0; i < 100000; builder.append(i), i++) ;
        long d1 = System.nanoTime();
        long bufferTime = f1 - f;
        long builderTime = d1 - d;
        System.out.println("StringBuilder的时间：" + builderTime + "纳秒。" +
                "\nStringBuffer的时间：" + bufferTime + "纳秒。");
        if (bufferTime > builderTime)
            System.out.println("StringBuilder比StringBuffer快");
        else if (bufferTime == builderTime)
            System.out.println("StringBuilder比StringBuffer速度一样");
        else System.out.println("StringBuilder比StringBuffer快");
    }
}

